package com.bdqn.tour.servlet.comments;

import com.bdqn.tour.dao.HotelImplementationClass;
import com.bdqn.tour.dao.impl.ImplUersDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/HotelReviews")
public class HotelReview extends HttpServlet {
    private static final int a = 4; // 每页显示的记录数
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8"); // 修改为 text/html

        int currentPage = 1;
        System.out.println("aa");
        try {
            if (req.getParameter("currentNo") != null) {
                currentPage = Integer.parseInt(req.getParameter("currentNo"));
                System.out.println(currentPage);
            }

            int a = 4; // 假设每页显示 10 条记录
            int start = (currentPage - 1) * a;
            int totalRecords = getTotalRecords(); // 获取总记录数
            double totalPages = ((double) totalRecords / a);

            List<com.bdqn.tour.entity.HotelReview> commentsList = getComments(start, a); // 获取当前页的评论数据

            req.getSession().setAttribute("commentsList", commentsList);
            req.setAttribute("currentPage", currentPage);
            req.setAttribute("totalPages", totalPages);

            req.getRequestDispatcher("backpinglunManagement.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException("Error processing request", e);
        }
    }
    /*
     * 获取总记录数
     */
    private int getTotalRecords() {
        // 从数据库中获取总记录数
        HotelImplementationClass commentDao = new ImplUersDao();
        return commentDao.getTotalCommentsCount1();

    }

    /**
     * 将方法封装为私有方法，方便调用
     * @param start 起始记录
     * @param limit 总数
     * @return 封装的数据
     */
    private List<com.bdqn.tour.entity.HotelReview> getComments(int start, int limit) {
        HotelImplementationClass commentDao = new ImplUersDao();
        return commentDao.getHotelReview(start, limit);
    }
}
